#-------------------------------------------
# Author:likezhang
# Email: likzhang@gmail.com
# Date: 12/13/2013
# All right reserved
#-------------------------------------------
from core import myapp
from bson.dbref import DBRef
from db.mongodb import DBConn
from cfg import cfg
import time

app = myapp


class TestData():
    
    def __init__(self):
        conn = DBConn()
        self.db = conn.get_database(cfg.database)

    def get_dbref(self, collection, _dict, fields=None):
        '''Arguments:
            fields -- dict type, additional fields
        '''
        db = self.db
        model = db.find_one(collection, _dict)
        if model is None:
            if fields is not None:
                for key in fields:
                    _dict[key] = fields[key]
            _id = db.save(collection, _dict)
            return DBRef(collection, _id)
        else:
            return DBRef(collection, model['_id'])

    def gen_datas(self):
        # The following are embedded datas, the embedded datas will be created when run tests
        # -------------------------------------
        # User
        # -------------------------------------
        user_dict = {'username':'zlk', 
                           'password':'zlk',
                           'email':'zlk@forlocal.com', 
                           'role':'1', #0 user, 1 admin
                           'phone': '519-334-6720',
                           'description':'admin @ forlocal.com'
        }
        
        # user is a document object
        self.user_ref = self.get_dbref('users', user_dict, {'created': time.time()})
        
        # -------------------------------------
        # Category
        # -------------------------------------
        category_dict = {'name': 'embedded test category',
                        'description': 'This is an embedded category'
                    }
        self.category_ref = self.get_dbref('categories', category_dict)
        
        # -------------------------------------
        # Subject
        # -------------------------------------    
        subject_dict = {'body': 'This is an embedded test subject'}
        # subject is a document object
        self.subject_ref = self.get_dbref('subjects', subject_dict)
        
        
        # -------------------------------------
        # Post
        # -------------------------------------
        post_dict = {'body': 'This is an embedded post',
                    'author_ref': self.user_ref,
                    'subject_ref': self.subject_ref
                    }
        # post is a document object
        self.post_ref = self.get_dbref('posts', post_dict, {'created': time.time()})

        # -------------------------------------
        # Comment
        # -------------------------------------
        comment_dict = {'body': 'This is an embedded comment',
                        'mode': 0, # comment
                        'author_ref': self.user_ref,
                        'post_ref': self.post_ref
                        }
        # comment is a document object
        self.comment_ref = self.get_dbref('comments', comment_dict, {'created': time.time()})

    
        # -------------------------------------
        # City
        # -------------------------------------
        city_dict = {'name':'London', 'province':'ON', 'country':'CA'}    
        self.city_ref = self.get_dbref('cities', city_dict)
        
        # -------------------------------------
        # Address
        # -------------------------------------  
        address_dict = {'unit':None, 'street':'128 Lincon St','city_ref': self.city_ref,'postcode':'N6G0C9'}
        self.address_ref = self.get_dbref('addresses', address_dict)
    
        # -------------------------------------
        # Company
        # -------------------------------------
        company_dict = {'name': 'embedded test company',
                        'description': 'This is an embedded company',
                        'address_ref': self.address_ref,
                        'admin_ref':self.user_ref
                    }
        # company is a document object
        self.company_ref = self.get_dbref('companies', company_dict)    

        # -------------------------------------
        # Item
        # -------------------------------------
        item_dict = {'name': 'embedded test item',
                           'description': 'This is an embedded item',
                           'price':12.99,
                           'company_ref': self.company_ref
                    }
        # item is a document object
        self.item_ref = self.get_dbref('items', item_dict) 


        # -------------------------------------
        # Order
        # -------------------------------------
        order_dict = {'title': 'embedded test order','user_ref': self.user_ref}
        # order is a document object
        self.order_ref = self.get_dbref('orders', order_dict, {'created': time.time()}) 

        # -------------------------------------
        # OrderItem
        # -------------------------------------
        order_item_dict = {'quantity': 2,
                           'item_ref': self.item_ref,
                           'order_ref':self.order_ref}
        # order_item is a document object
        self.order_item_ref = self.get_dbref('order_items', order_item_dict) 


        # -------------------------------------
        # Picture
        # -------------------------------------
        picture_dict = {'name': 'embedded test picture',
                        'description': 'This is an embedded picture',
                        'fname': 'embedded-test.jpg',
                        'order':0,
                        'item_ref':self.item_ref
                    }
        # picture is a document object
        self.picture_ref = self.get_dbref('pictures', picture_dict, {'created': time.time()})  

        ###################################
        # Non embedded datas
        ###################################
        self.new_city = {'name':'test city', 'province':'ON', 'country':'CA'}
         
        self.new_address = {'unit':None,'street':'128 test street name','city_ref': self.city_ref, 'postcode':'N6G0C9'}
        
        self.new_user = {'username':'test', 
                   'password':'test',
                   'email':'test@forlocal.com', 
                   'role':0, #0 admin, 1 user
                   'phone': '519-334-6720',
                   'description':"Local business"
        }
        
        self.new_subject = {'body': 'This is a test subject'}
        
        self.new_post = {'body': 'This is a test post',
            'author_ref': self.user_ref,
            'subject_ref': self.subject_ref
            }    
        
        self.new_comment = {'body': 'This is a test comment',
                       'mode': 0,
                       'author_ref': self.user_ref,
                       'source_ref': self.post_ref
                       }
        
        self.comment_reply = {'body': 'This is a test comment reply',
                   'mode': 1,
                   'author_ref': self.user_ref,
                   'source_ref': self.comment_ref
                   }
        
        self.new_company = {'name': 'test company',
                           'description': 'This is a test company',
                           'address_ref': self.address_ref,
                           'admin_ref':self.user_ref
                    }

        self.new_item = {'name': 'test item',
                         'description': 'This is a test item',
                         'price':8.99,
                         'company_ref': self.company_ref
                    }
        
        self.new_category = {'name': 'test category',
                         'description': 'This is a test category'
                    }
        
        self.new_order = {'title': 'test order', 'user_ref': self.user_ref}

        self.new_order_item = {'quantity': 2, 'item_ref': self.item_ref,'order_ref': self.order_ref}

        self.new_picture = {'name': 'test picture',
                        'description': 'This is a test picture',
                        'fname': 'test.jpg',
                        'order':0,
                        'item_ref':self.item_ref
                    }


        self.address_form = {'mode':'new',
                            'unit': self.new_address['unit'],
                            'street': self.new_address['street'],
                            'postcode': self.new_address['postcode'],
                            'city': city_dict['name'],
                            'province': city_dict['province'],
                            'country':city_dict['country']    
        }

        self.company_form = {'mode':'new',
                            'name': self.new_company['name'],
                            'description': self.new_company['description'],
                            'unit': address_dict['unit'],
                            'street': address_dict['street'],
                            'postcode': address_dict['postcode'],
                            'city': city_dict['name'],
                            'province': city_dict['province'],
                            'country':city_dict['country'],
                            'admin_id':self.user_ref.id    
        }

        self.category_form = {'mode':'new',
                    'name': self.new_category['name'],
                    'description': self.new_category['description']
                    }

        # new form
        self.new_item_form = {'mode':'new',
                          'name': self.new_item['name'],
                         'description': self.new_item['description'],
                         'price':self.new_item['price'],
                         'company_ref': self.company_ref,
                         'iid':''
                    }

        self.edit_item_form = {'mode':'edit',
                          'name': self.new_item['name'],
                         'description': self.new_item['description'],
                         'price':self.new_item['price'],
                         'company_ref': self.company_ref,
                         'iid':str(self.item_ref.id)
                    }


    """
    house = db.find_one('houses', 
            {'name':'my house',
               'description':'my house description',
               'year': '2003',
               'price':'230000', 
               'status':'0', 
               'style':'1', 
               'n_rooms':'3', 
               'n_baths':'3', 
               'n_garages':'2',
               'basement':'1',
               'lots':'40',
               'living_space':'3000',
               'faces':'0', 
               'deck':'1',
               'patio':'0',
               'fence':'1',
               'ac':'1',
               'address':address_ref})
    if house is not None:
        house_ref = DBRef('houses', house['_id'])

    category = db.find_one('categories', {'name':'house'})
    category_ref = DBRef('categories', category['_id'])

    advertisement = db.find_one('advertisements',
                    { 'prefer_contact':0,#0 email, 1 phone
                       'email':'likzhang@gmail.com',
                       'phone': '519-572-0859',
                       'type':0, # 0 rent, 1 sell
                       'category':category_ref,
                       'item':house_ref,
                       'user':user_ref})
    
    if advertisement is not None:
        advertisement_ref = DBRef('advertisements', advertisement['_id'])
    
    
    picture = db.find_one('pictures',{'name':'my house picture',
               'description':'my house outside',
               'fname': r'my.jpg',
               'order':0, # 0 rent, 1 sell
               'category':category_ref,
               'item':house_ref})
    
    if picture is not None:
        picture_ref = DBRef('pictures', picture['_id'])
    """
    
test_data = TestData()
test_data.gen_datas()